package repository;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;

import util.ConnectionFactory;
import exceptions.ExceptionSql;

public class RepositorioControle implements IRepositorioControle {

	// ---------------------------------------------------------------------------------
	public boolean limparBanco() throws SQLException {

		String sql = "SET FOREIGN_KEY_CHECKS=0";
		String sql1 = "DELETE FROM usuario";
		String sql2 = "DELETE FROM bem_segurado";
		String sql3 = "DELETE FROM cobertura";
		String sql4 = "DELETE FROM condutor";
		String sql5 = "DELETE FROM endereco";
		String sql6 = "DELETE FROM parcela";
		String sql7 = "DELETE FROM pagamento";
		String sql8 = "DELETE FROM segurado";
		String sql9 = "DELETE FROM cliente";
		String sql10 = "SET FOREIGN_KEY_CHECKS=1";

		// conectando
		Connection conLocal = new ConnectionFactory().getConnection();

		PreparedStatement stm = (PreparedStatement) conLocal
				.prepareStatement(sql);
		stm.execute();
		stm.close();

		PreparedStatement stm1 = (PreparedStatement) conLocal
				.prepareStatement(sql1);
		stm1.execute();
		stm1.close();

		PreparedStatement stm2 = (PreparedStatement) conLocal
				.prepareStatement(sql2);
		stm2.execute();
		stm2.close();

		PreparedStatement stm3 = (PreparedStatement) conLocal
				.prepareStatement(sql3);
		stm3.execute();
		stm3.close();

		PreparedStatement stm4 = (PreparedStatement) conLocal
				.prepareStatement(sql4);
		stm4.execute();
		stm4.close();

		PreparedStatement stm5 = (PreparedStatement) conLocal
				.prepareStatement(sql5);
		stm5.execute();
		stm5.close();

		PreparedStatement stm6 = (PreparedStatement) conLocal
				.prepareStatement(sql6);
		stm6.execute();
		stm6.close();

		PreparedStatement stm7 = (PreparedStatement) conLocal
				.prepareStatement(sql7);
		stm7.execute();
		stm7.close();

		PreparedStatement stm8 = (PreparedStatement) conLocal
				.prepareStatement(sql8);
		stm8.execute();
		stm8.close();

		PreparedStatement stm9 = (PreparedStatement) conLocal
				.prepareStatement(sql9);
		stm9.execute();
		stm9.close();

		PreparedStatement stm10 = (PreparedStatement) conLocal
				.prepareStatement(sql10);
		stm10.execute();
		stm10.close();

		conLocal.close();
		return true;

	}

	// ---------------------------------------------------------------------------------
	public boolean testeConexaoIntranet() throws SQLException {

		try {

			Connection conIntranet = new ConnectionFactory()
					.getConnection();

			if (conIntranet.isValid(0)) {
				conIntranet.close();
				return true;
			}
		} catch (Exception e) {
			new ExceptionSql();
		}

		return false;
	}

	// ---------------------------------------------------------------------------------
	public boolean testeConexaoLocal() throws SQLException {

		try {

			Connection conLocal = new ConnectionFactory().getConnection();

			if (conLocal.isValid(0)) {
				conLocal.close();
				return true;
			}

		} catch (Exception e) {
			new ExceptionSql();
		}

		return false;
	}

	// ---------------------------------------------------------------------------------
}
